package com.cyt.web.controller.work.association;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.ErrorCode;
import com.cyt.work.association.model.po.WxAssociation;
import com.cyt.work.association.model.vo.WxAssociationVo;
import com.cyt.work.association.service.WxAssociationService;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.web.controller.base.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 社团管理-社团管理控制层
 *
 * @author han jun
 * @date 2019-8-30 16:06:11
 */
@RestController
public class WxAssociationController extends BaseController {

    @Resource
    private WxAssociationService wxAssociationService;

    /**
     * 统一跳转
     *
     * @param pageName 页面名称
     * @param applyId 应用id
     * @return ModelAndView
     * @author shengQiang Yu
     * @date 2019-08-28 15:17:40
     */
    @RequestMapping("/work/association/wxassociation/{pageName}")
    public ModelAndView goPage(@PathVariable("pageName") String pageName, String applyId) {
        ModelAndView view = new ModelAndView("/work/association/wxassociation/"+pageName);
        view.addObject("applyId",applyId);
        return view;
    }

    /**
     * 分页查询
     *
     * @param wxAssociationVo 接受参数对象
     * @param page 当前页面
     * @param limit 页面数量
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:06:11
     */
    @BaseLog(module = "社团管理", classz = "社团管理", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/association/wxassociation/page", method = RequestMethod.POST)
    public ReturnResult page(WxAssociationVo wxAssociationVo, int page, int limit) {
        return wxAssociationService.page(wxAssociationVo, page, limit);
    }


    /**
     * 获取社团的集合
     *
     * @param wxAssociationVo
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/9/5 10:56
     */
    @BaseLog(module = "社团管理", classz = "社团管理", methods = "查询社团集合", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/association/wxassociation/getList", method = RequestMethod.POST)
    public ReturnResult getList(WxAssociationVo wxAssociationVo,String selectIds) {
        List<WxAssociation> newlist = new ArrayList<>();
        List<WxAssociation> list = wxAssociationService.listExample(wxAssociationVo);
        if (selectIds!=null&&selectIds.length()>0){
            String[] ids  =selectIds.split(",");
            for (WxAssociation wxAssociation:list){
                if (!Arrays.asList(ids).contains(wxAssociation.getId())){
                    newlist.add(wxAssociation);
                }
            }
            return new ReturnResult(newlist);
        }else {
            return new ReturnResult(list);
        }

    }


    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:06:11
     */
    @BaseLog(module = "社团管理", classz = "社团管理", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/association/wxassociation/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String id,String applyId) {
        ModelAndView view = new ModelAndView("/work/association/wxassociation/wxAssociationEdit");
        if (StringUtils.isNotBlank(id)) {
            view.addObject(wxAssociationService.selectByPrimaryKey(id));
        }
        view.addObject("applyId",applyId);
        view.addObject("id",id);
        return view;
    }




    /**
     * 跳转详情界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:06:11
     */
    @BaseLog(module = "社团管理", classz = "社团管理", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/association/wxassociation/getDetail", method = RequestMethod.GET)
    public ModelAndView getDetail(String id,String applyId) {
        ModelAndView view = new ModelAndView("/work/association/wxassociation/wxAssociationDetail");
        if (StringUtils.isNotBlank(id)) {
            view.addObject(wxAssociationService.selectByPrimaryKey(id));
        }
        view.addObject("applyId",applyId);
        return view;
    }


    /**
     * 保存或更新
     *
     * @param request 请求对象
     * @param wxAssociationVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:06:11
     */
    @BaseLog(module = "社团管理", classz = "社团管理", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/association/wxassociation/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, WxAssociationVo wxAssociationVo) {
        return wxAssociationService.addOrUpdate(wxAssociationVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件逻辑删除
     *
     * @param request 请求对象
     * @param wxAssociationVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:06:11
     */
    @BaseLog(module = "社团管理", classz = "社团管理", methods = "逻辑删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/association/wxassociation/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, WxAssociationVo wxAssociationVo) {
        return wxAssociationService.remove(wxAssociationVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件物理删除
     *
     * @param request 请求对象
     * @param wxAssociationVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:06:11
     */
    @BaseLog(module = "社团管理", classz = "社团管理", methods = "物理对象对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/association/wxassociation/delete", method = RequestMethod.GET)
    public ReturnResult delete(HttpServletRequest request, WxAssociationVo wxAssociationVo) {
        return wxAssociationService.delete(wxAssociationVo, acceptCurrentUser(request));
    }



}